package ad_exercise_3.model;

import java.util.ArrayList;
import java.util.Iterator;

import ad_exercise_3.controller.Game;
import ad_exercise_3.controller.GameHelper;
import ad_exercise_3.model.Card.CardColour;

/**
 * Represents a player controller by a human.
 * 
 * @author Karen Berntsen (s0815799)
 * @author Erik Stens (s0615374)
 */
public class HumanPlayer extends Player {
	
	public HumanPlayer(CustomList<Card> initialHand, Game game) {
		super(initialHand, game);
	}
	
	public HumanPlayer(String name, CustomList<Card> initialHand, Game game) {
		super(name, initialHand, game);
	}
	
	@Override
	protected boolean keepCard(Card card) {
		System.out.println("You took " + card + " from the stack");
		if (game.cardFits(card)) {
			String result = GameHelper.getUserInput("Do you want to play this card?", "y", "n", "yes", "no");
			
			return result.equals("n") || result.equals("no");
		} else {
			System.out.println("This card doesn't fit on the play stack! Putting the card in your hand...");
			return false;
		}
	}
	
	@Override
	public Card askCard() {
		if (!game.emptyPlayStack()) {
			System.out.println("The current card on the play stack is " + game.getCurrentCard());
		} else {
			System.out.println("There is no card on the play stack yet, any card will fit");
		}
		
		System.out.println("Currently these are the contents of your hand: ");
		toonHand();
		
		ArrayList<String> optionList = new ArrayList<String>();
		
		Iterator<Card> handIterator = hand.iterator();
		Card card;
		int index = 0;
		while (handIterator.hasNext()) {
			card = handIterator.next();
			if (game.cardFits(card)) {
				optionList.add(Integer.toString(index));
			}
			index++;
		}
		if (optionList.isEmpty()) {
			System.out.println("None of your cards fits on the play stack. Taking a card...");
			return null;
		}
		optionList.add("none");
		
		String result = GameHelper.getUserInput("Which card do you want to play? These are your options: " + optionList, optionList);
		if (result.equals("none")) {
			return null;
		} else {
			return hand.remove(Integer.parseInt(result));
		}
	}
	
	@Override
	public CardColour askColour() {
		return CardColour.valueOf(GameHelper.getUserInput("Choose a colour for the choice card:", CardColour.RED, CardColour.YELLOW, CardColour.GREEN, CardColour.BLUE).toUpperCase());
	}
	
	@Override
	public String getType() {
		return "human";
	}
}
